(() => {
    console.log("v0.1")
    const btn = document.createElement("button")
    document.body.appendChild(btn);
    btn.style.position = "fixed";
    btn.style.right = "200px";
    btn.style.bottom = "200px";
    const submitBtn = document.createElement("button")
    submitBtn.style.position = "fixed"
    submitBtn.style.right = "100px";
    submitBtn.style.bottom = "100px";
    submitBtn.innerText = "提交并保存"
    document.body.appendChild(submitBtn)
    let stat = false;
    let time;
    const hash = new Set();
    const queue = [];
    const getTable = () => {
        const cards = Array.from(document.getElementsByClassName("ant-row-flex ant-row-flex-start ant-row-flex-bottom")[0].children);
        const data = cards.map((card) => {
            const [info, context, btnBox] = Array.from(card.children[0].children);
            const msg = info.innerText.split("\n");
            // const userId = msg[msg.length - 1];
            const commentId = msg[msg.length - 2];
            const videoId = msg[msg.length - 3];
            const id = encodeURI(`${commentId}_${videoId}`);
            const btns = Array.from(btnBox.children)
            btns.map(e => e.children[0]);
            return { id, btns };
        })
        return data;
    }
    const onSubmit = () => {
        if (!queue.length) return;
        const { id, btns } = queue[0];
        queue.shift();
        const ans = btns.findIndex(e => e.className.includes("primary"));
        if (ans === -1) return;
        fetch(`http://114.115.166.23:14520/qq/zljbz/${id}/${ans}/set`)
        onSubmit();
    }
    const get = ({ id, btns }) => {
        if (hash.has(id)) return;
        const spans = btns.map(e => e.children[0]);
        if (spans.length === 0) return;
        hash.add(id);
        queue.push({ id, btns })

        fetch(`http://114.115.166.23:14520/qq/zljbz/${id}/get`)
            .then(e => e.json()).then((e) => {
                const type = new Array(100).fill(0);
                for (const i of e) type[i]++;
                for (let i = 0; i < spans.length; i++) {
                    spans[i].innerText = [spans[i].innerText.split("-")[0], type[i]].join("-")
                }
            }).catch((e) => {
                hash.delete(id)
            })
    }

    const start = () => {
        btn.innerText = "暂停"
        time = setInterval(() => {
            const data = getTable();
            for (const item of data) {
                get(item);
            }
        }, 1000);
    }
    const end = () => {
        btn.innerText = "点击开始"
        clearInterval(time);
    }
    const click = () => {
        stat = !stat;
        if (stat) start()
        else end();
    }
    const onFinish = () => {
        onSubmit()
        setTimeout(() => {
            Array.from(document.getElementsByTagName("button")).find(e => e.innerText === "提交").click()
        }, 500)
    }
    submitBtn.onclick = () => onFinish
    document.addEventListener("keydown", function (event) {
        if (event.key === " ") {
            // 执行你想要执行的函数
            onFinish()
        }
    });
    btn.onclick = click;
    end()
})()